package mate.business
{
	import com.asfusion.mate.actionLists.Scope;
	import com.asfusion.mate.core.GlobalDispatcher;
	
	import flash.events.Event;
	
	import mate.events.LoginEvent;
	
	import mx.collections.ArrayCollection;
	
	import spark.components.NavigatorContent;
	
	public class LoginManager
	{
		
		import classespinkpony.*;
		
		import mx.controls.Alert;
		import mx.rpc.Fault;
		
		private var _user:User;
		private var _loggedIn:Boolean = false;
		
		
		[Bindable] 
		public var dispatcher:GlobalDispatcher;
		
		public function LoginManager ( )
		{
			dispatcher = new GlobalDispatcher();
		}
		
		[Bindable]
		public function get loggedIn():Boolean
		{
			return _loggedIn;
		}

		private function set loggedIn(value:Boolean):void
		{
			_loggedIn = value;
		}

		public function set user(user:User):void{
			this._user = user;
		}
		
		[Bindable]
		public function get user():User{
			return this._user;
		}
		
		[Bindable]
		public var selectedView: int = 0;
		
		public function storeUser(users:Object):void
		{		
			
			if(users != null){
				for each (var user:User in users)
				{
					this.user = user;
					if(user.id > 0){
						loggedIn = true;
					}
					showControlPanel(user);				
				}
			}
			
			if(!loggedIn){
				Alert.show("Fout bij het inloggen! Probeer het opnieuw!");
			}
		}
		
		public function logout():void
		{
			this.user = null;
			loggedIn = false;
		}
		
		
		public function showControlPanel(user:User):void
		{
			dispatcher.dispatchEvent(new LoginEvent(LoginEvent.LOADCONTROLPANEL,user));
		}
		
		public function handleFault(fault:Fault):void{
			Alert.show(fault.faultString, fault.faultCode.toString());
		}
	}
}